﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BeanboxSoftware.BeanProxy.Recording;

namespace BeanboxSoftware.BeanProxy
{
  public partial class MethodConfig
  {
    public ExpectedCall Listen(In recordingType)
    {
      return Listen(1, recordingType);
    }

    public ExpectedCall Listen(int count, In recordingType)
    {
      var ec = new ExpectedCallAccess(this, recordingType);
      var mr = new MethodRule(count, ec);
      Target.MethodGroup.AddRule(mr);
      return ec;
    }
  }

  public partial class MethodConfig<C>
  {
    public ExpectedCall<C> Listen(In recordingType)
    {
      return Listen(1, recordingType);
    }
    public ExpectedCall<C> Listen(int count, In recordingType)
    {
      return new ExpectedCall<C>((ExpectedCallAccess)Access.Listen(count, recordingType));
    }
    public ExpectedCall<C> Listen(In recordingType, C thenAction)
    {
      return Listen(1, recordingType, thenAction);
    }
    public ExpectedCall<C> Listen(int count, In recordingType, C thenAction)
    {
      Func<object[], object> oaofp = va =>
      {
        var fp = (MulticastDelegate)((object)thenAction);
        return fp.Method.Invoke(fp.Target, va);
      };

      return ListenGeneric(count, recordingType, oaofp);
    }
    public ExpectedCall<C> ListenGeneric(int count, In recordingType, Func<object[], object> genericThenAction)
    {
      var eca = (ExpectedCallAccess)Access.Listen(count, recordingType);

      eca.ExpectedReturn = new ExpectedReturnAccess(eca);
      eca.ExpectedReturn.Then = genericThenAction;

      var ec = new ExpectedCall<C>(eca);
      return ec;
    }
  }
}
